package com.auxgroup.hr.front.site.co.personnel;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import com.auxgroup.hr.front.site.vo.person.JobCurrQueVO;
import com.auxgroup.hr.front.site.vo.person.JobCurrRespVO;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.auxgroup.hr.front.commons.BaseRetCode;
import com.auxgroup.hr.front.commons.vo.BaseResponseVO;
import com.auxgroup.hr.front.dao.domain.EmplPhotoPrimary;
import com.auxgroup.hr.front.dao.domain.JobCurrPrimary;
import com.auxgroup.hr.front.dao.domain.PsEmplPhoto;
import com.auxgroup.hr.front.dao.domain.PsCJobCurrVw;
import com.auxgroup.hr.front.dozer.IObjectMapper;
import com.auxgroup.hr.front.service.personnal.EmplPhotoService;
import com.auxgroup.hr.front.service.personnal.JobCurrService;
import com.auxgroup.hr.front.site.co.BaseCO;
import com.auxgroup.hr.front.site.vo.person.JobCurrQueVO;
import com.auxgroup.hr.front.site.vo.person.JobCurrRespVO;

/**
 * 个人职级信息表CO
 * 
 * @author wangchangyun
 * @version 创建时间：180523
 */
@RestController
@RequestMapping("${auxgroup.hr.contextPath}/jobCurr")
public class JobCurrCO extends BaseCO {

	private static final Logger logger = LoggerFactory.getLogger(JobCurrCO.class);

	@Autowired
	public JobCurrService jobCurrService;
	
	@Autowired
	public EmplPhotoService emplPhotoService;

	@Resource(name = "dozerMapper")
	protected IObjectMapper objectMapper;

	/**
	 * 获取个人职级信息
	 * @param hireId
	 * @return
	 */
	@GetMapping(value = "/getOne")
	public BaseResponseVO getOne(JobCurrPrimary jobPrimary) {
		logger.info("getPersonalData param= {}", jobPrimary);
		if(StringUtils.isBlank(jobPrimary.getEmplid())){
			return buildRespone(BaseRetCode.CODE_ERROR_REQUEST_PARAM_NULL);
		}
		List<PsCJobCurrVw> jobCurrs = jobCurrService.getJobCurr(jobPrimary);
		List<JobCurrRespVO> respVOs = new ArrayList<JobCurrRespVO>();
		buildPhotoUrl(jobCurrs,respVOs);
		buildUnderLineSize(respVOs,"DIRECT");//添加下属个数属性
		return buildRespone(respVOs,false);//该员工有多个职位
	}
	
	/**
	 * 获取下属信息
	 * @param hireId
	 * @return
	 */
	@GetMapping(value = "/getUnderLine")
	public BaseResponseVO getSuperVisor(@Valid JobCurrQueVO requeVo, BindingResult binding) {
		logger.info("getPersonalData param= {}", requeVo);
		 // 表单校验
        super.assertValidation(binding);
		List<PsCJobCurrVw> jobCurrs = jobCurrService.getSuperVisor(requeVo.getEmplid(), requeVo.getEmplRcd(),requeVo.getUnderLineType());
		List<JobCurrRespVO> respVOs = new ArrayList<JobCurrRespVO>();
		buildPhotoUrl(jobCurrs,respVOs);//添加图片url属性
		buildUnderLineSize(respVOs,requeVo.getUnderLineType());//添加下属个数属性
		return buildRespone(respVOs,false);
	}
	
	
	/**
	 * 设置头像url
	 * @param jobCurrs
	 * @param respVOs
	 */
	private void buildPhotoUrl(List<PsCJobCurrVw> jobCurrs,List<JobCurrRespVO> respVOs){
		JobCurrRespVO  respVO = null;
		if(jobCurrs != null&&jobCurrs.size()>=1){
			for (PsCJobCurrVw jobcurr : jobCurrs) {
				respVO = objectMapper.map(jobcurr, JobCurrRespVO.class);
				PsEmplPhoto photo = emplPhotoService.getEmplPhoto(new EmplPhotoPrimary(jobcurr.getEmplid(), "PAGE"));
				if(photo!=null){
					respVO.setPhotoUrl(emplPhotoService.buildEmplPhoto(photo));
				}
				respVOs.add(respVO);
			}
		}
	}
	
	/**
	 * 添加下属数量属性。 要在设置头像url 方法之后
	 * @param respVOs
	 */
	private void buildUnderLineSize(List<JobCurrRespVO> respVOs,String underLineType){
		for (JobCurrRespVO jobCurrRespVO : respVOs) {
			jobCurrRespVO.setUnderLineSize(jobCurrService.getSuperVisorSize(jobCurrRespVO.getEmplid(), jobCurrRespVO.getEmplRcd(), underLineType));
		}
	}
	
	
}
